//
//  ResidentListViewController.m
//  homeland
//
//  Created by Alawn on 13-11-1.
//  Copyright (c) 2013年 xzysun. All rights reserved.
// 

#import "ResidentListViewController.h"
#import "ResidentDataService.h"
#import "AdvisoryListCell.h"
#import "ResidentComplainRecordController.h"
#import "UserInfoService.h"
@interface ResidentListViewController ()
@property (strong, nonatomic) ResidentDataService *dataService;
@property (strong, nonatomic) CustomPullToRefresh *ptr;
@end

@implementation ResidentListViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"信访记录";
//    CommonMineQueryItem *item = [_config objectForKey:@"dataItem"];
    self.dataService = [ResidentDataService new];
    _dataService.delegate = self;
    //控制搜索栏
    self.searchBarForNo.delegate = self;
    self.searchBarForNo.inputAccessoryView = [self getInputAccessoryView];
    self.searchBarForName.delegate = self;
    self.searchBarForName.inputAccessoryView = [self getInputAccessoryView];
    //开启下拉刷新
    self.ptr = [[CustomPullToRefresh alloc]initWithScrollView:self.tableView delegate:self];
    //开始读取数据
//     [_dataService loadDataWithNoAndName:_searchBarForNo.text withName:_searchBarForName.text];
//    [self startLoadingView];
    UserInfoService *userInfo=[UserInfoService getUserInfoService];
    if (userInfo.currentLoginUserName && userInfo.currentLoginUserName.length>0) {
        self.labelMsg.text=@"如您在个人电脑上提交的记录没有在此显示，请输入当时的受理编号以及信访人姓名查询";
    }
    else
    {
        self.labelMsg.text=@"您未登录，请输入受理编号以及信访人姓名查询";
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table Methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataService.dataList?[_dataService.dataList count]:0;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellIdentifier = @"AdvisoryListCell";
    AdvisoryListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"AdvisoryListCell" owner:self options:nil] objectAtIndex:0];
    }
    ResidentDataItem *item = [_dataService.dataList objectAtIndex:indexPath.row];
    cell.codeLabel.text = item.slid;
    if (item.replyStatus && item.replyStatus != (id)[NSNull null] && item.replyStatus.integerValue == 1) {
        cell.statusLabel.text = @"已回复";
    } else {
        cell.statusLabel.text = @"未回复";
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ResidentDataItem *item = [_dataService.dataList objectAtIndex:indexPath.row];
    ResidentComplainRecordController *detailVC = [[ResidentComplainRecordController alloc]initWithNibName:@"ResidentComplainRecordController" bundle:nil];
    detailVC.item=item;
    [self.navigationController pushViewController:detailVC animated:YES];
}

#pragma mark - Search Bar Delegate
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    NSLog(@"search bar input:%@",searchText);
}

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    NSLog(@"search button clicked with input:%@",searchBar.text);
    [searchBar resignFirstResponder];
}

-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
    NSLog(@"search bar end edit with input %@",searchBar.text);
//    [self startLoadingView];
//    [_dataService loadDataWithNoAndName:_searchBarForNo.text withName:_searchBarForName.text];
}
#pragma mark - Button
- (IBAction)doSearch:(id)sender
{
    [_searchBarForName resignFirstResponder];
    [_searchBarForNo resignFirstResponder];
    if (_searchBarForName.text.length == 0) {
        [_searchBarForName becomeFirstResponder];
        [self showAlert:@"请输入信访人姓名"];
        return;
    }
    if (_searchBarForNo.text.length == 0) {
        [_searchBarForNo becomeFirstResponder];
        [self showAlert:@"请输入受理编号"];
        return;
    }
    [self startLoadingView];
    [_dataService loadDataWithNoAndName:_searchBarForNo.text withName:_searchBarForName.text];
}
-(void)showAlert:(NSString *) msg
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}

#pragma mark - Data Service Delegate
-(void)loadDataFinished
{
    [self stopLoadingView];
    [self.ptr endRefresh];
    [self.tableView reloadData];
}

-(void)loadDataFaild:(NSString *)msg
{
    [self stopLoadingView];
    [self.ptr endRefresh];
     [self.tableView reloadData];
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"错误" message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}

-(void)setEnableToLoadMore:(BOOL)enable
{
    self.ptr.enablePullToBottom = enable;
}

#pragma mark - PullToRefresh Method
//捕获到拉动事件
-(void)customPullToRefreshShouldRefresh:(CustomPullToRefresh *)ptr withDirection:(MSRefreshDirection)direction
{
    NSLog(@"捕获到拉动事件");
    if (direction == MSRefreshDirectionTop) {
        [_dataService loadDataWithNoAndName:_searchBarForNo.text withName:_searchBarForName.text];
    }
    if (direction == MSRefreshDirectionBottom) {
        [_dataService loadDataWithNoAndName:_searchBarForNo.text withName:_searchBarForName.text];
    }
}
@end
